/*
 * Name: Joon Ho Cho
 * Date: 10/24/2008
 * Course: 15-437
 */
package scheduler.controller;

import javax.servlet.http.HttpServletRequest;


import org.mybeans.dao.DAOException;

import scheduler.databean.Bookmark;
import scheduler.model.BookmarkDao;
import scheduler.model.Model;
import scheduler.model.UserDao;


public class BrowseAction extends Action {
	private BookmarkDao bookmarkDao;
	private UserDao userDao;

    public BrowseAction(Model model) {
    	bookmarkDao = model.getBookmarkDao();
    	userDao = model.getUserDao();
	}

    public String getName() { return "browse.do"; }

    public String perform(HttpServletRequest request) {
    	try {
            request.setAttribute("bookmarks", null);
            String userName = request.getParameter("user");

            Bookmark[] bookmarks = bookmarkDao.getBookmarksForUser(userName);

            if (bookmarks.length <= 0) {
        		request.setAttribute("message",userName + " does not have any bookmarks.");
        		return "browse.jsp";
            }
            
            request.setAttribute("bookmarks", bookmarks);

			request.setAttribute("registeredUsers", userDao.getAll());
            return "browse.jsp";
            
    	} catch (DAOException e) {
        	e.printStackTrace();
        	request.setAttribute("dbError",e.getMessage());
        	return "db-error.jsp";
    	}
    }
}
